package org.example.com.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.com.mapper.HouseMapper;
import org.example.com.pojo.House;
import org.example.com.pojo.PageResult;
import org.example.com.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public List<House> getNearbyHouses() {
        log.info("获取推荐房源列表");
        return houseMapper.findNearbyHouses();
    }

    @Override
    public PageResult searchHouses(Integer page, Integer limit, String keyword, 
                                  String provinceId, String cityId, String districtId,
                                  String minRent, String maxRent, String paymentType,
                                  String orderBy, String orderType) {
        log.info("搜索房源列表，页码：{}，每页条数：{}，关键词：{}", page, limit, keyword);
        
        // 计算偏移量
        Integer offset = (page - 1) * limit;
        
        // 查询房源列表
        List<House> houses = houseMapper.searchHouses(keyword, provinceId, cityId, districtId,
                minRent, maxRent, paymentType, orderBy, orderType, offset, limit);
        
        // 查询总数
        Long total = houseMapper.countHouses(keyword, provinceId, cityId, districtId,
                minRent, maxRent, paymentType);
        
        return new PageResult(total, houses);
    }

    @Override
    public House getHouseDetail(Integer id) {
        log.info("获取房源详情，ID：{}", id);
        return houseMapper.findById(id);
    }

    /**
     * 新增房源
     * @param house
     */
    @Override
    public void addHouse(House house) {
        houseMapper.add(house);
    }
}
